<?php
/**
 * User: hjd-yao
 * Date: 2019/5/8
 * Time: 19:54
 */

namespace App\Exports;


use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\BeforeWriting;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class DataExport extends DefaultValueBinder implements
    WithCustomValueBinder, WithEvents,
    FromCollection,WithHeadings,WithColumnFormatting,WithTitle,ShouldAutoSize,WithMapping
{
    use Exportable,RegistersEventListeners,ExportTrait;


    private $data;

    public function __construct()
    {
    }


    public static function beforeWriting(BeforeWriting $event)
    {

    }

    public static function beforeSheet(BeforeSheet $event)
    {
        $event->sheet->formatColumn('A','@');
    }

    public static function afterSheet(AfterSheet $event)
    {
        $event->sheet->formatColumn('A','@');
    }


    public function filterData($data,callable $callback)
    {
        $this->data = $callback($data);
        return $this;
    }

    public function headings(): array
    {
        return $this->headings;
    }

    public function map($row): array
    {
        return is_array($row)?$row:$row->toArray();
    }

    public function collection()
    {
        return $this->data;
    }

    public function columnFormats(): array
    {
        return [];
    }

    public function bindValue(Cell $cell, $value)
    {
//        if(!empty($this->columnFormat)){
//            foreach ($this->columnFormat as $key=>$data_type){
//                if($cell->getColumn() == $key){
//                    $cell->setValueExplicit($value, $data_type);
//                    return true;
//                }
//            }
//        }
//
////        if($cell->getColumn() == 'A'){
////            $cell->setValueExplicit($value, DataType::TYPE_STRING2);
////            return true;
////        }
        return parent::bindValue($cell, $value);
    }


    public function title(): string
    {
        return $this->title;
    }
}